package com.shicijiaoliu.service.impl;

import com.github.pagehelper.PageHelper;
import com.shicijiaoliu.mapper.Activemapper;
import com.shicijiaoliu.pojo.Active;
import com.shicijiaoliu.service.Activeservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class Activeserviceimpl implements Activeservice {

    @Autowired
    Activemapper activemapper;

    @Override
    public int getcount(String name, String starttime, String endtime) {
        LocalDateTime stime = null;
        LocalDateTime etime = null;
        if(starttime != null){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            stime = LocalDateTime.parse(starttime, fmt);
        }
        if(endtime != null){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            etime = LocalDateTime.parse(endtime, fmt);
        }
        return activemapper.getcount(name,stime,etime);
    }

    @Override
    public void add(Active active) {
        activemapper.add(active);

    }

    @Override
    public List<Active> findBypage(int pagecount, int pagenumber, String name, String starttime, String endtime) {
        LocalDateTime stime = null;
        LocalDateTime etime = null;
        if(starttime != null){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            stime = LocalDateTime.parse(starttime, fmt);
        }
        if(endtime != null){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            etime = LocalDateTime.parse(endtime, fmt);
        }
        int count = activemapper.getcount(null,null,null);
        int totalpage = (int) Math.ceil((double)count/pagenumber);
        if(pagecount>totalpage || pagecount <= 0){
            return null;
        }else{
            PageHelper.startPage(pagecount,pagenumber);
            return activemapper.findBypage(name,stime,etime);
        }
    }

    @Override
    public void update(Active active) {
        activemapper.update(active);
    }

    @Override
    public void delByid(long id) {
        activemapper.delByid(id);
    }

    @Override
    public Active findByid(long id) {
        return activemapper.findByid(id);
    }

    @Override
    public Active findByname(String name) {
        return activemapper.findByname(name);
    }

    @Override
    public List<Active> getlist() {
        return activemapper.getlist();
    }
}
